<template>
  <div class="app-container">
    <div v-show="!licenseInfo.visible && !productInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <new-page-filter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
          :slip-select-request="slipSelectRequest"
        >
        </new-page-filter>
      </div>
      <new-page-filter :filter-btn="filterInfo.btnList">
        <el-button v-show="true" @click="exportExcel">导出excel</el-button>
      </new-page-filter>
      <modelTips
        :items="modelTipsInfo.items"
        :title="modelTipsInfo.title"
        :brackets="true"
      />
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :tab-index="true"
        :check-box="false"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassName"
        @handleEvent="handleEvent"
        @pageInfo="pageInfo"
      >
        <!-- 证照插槽 -->
        <template v-slot:col-licenseNumber="scope">
          <p
            v-if="$permission('productExpiryWarningLicense:details')"
            class="slip-edit"
            @click="showLicenseDetail(scope.row.licenseId)"
          >
            {{ scope.row.licenseNumber }}
          </p>
          <p v-else>{{ scope.row.licenseNumber }}</p>
        </template>
        <!-- 通用名称插槽 -->
        <template v-slot:col-commonName="scope">
          <p
            v-if="$permission('productExpiryWarningProduct:details')"
            class="slip-edit"
            @click="showProductDetail(scope.row.productId)"
          >
            {{ scope.row.commonName }}
          </p>
          <p v-else>{{ scope.row.commonName }}</p>
        </template>
      </table-container>
    </div>
    <license-details
      v-if="licenseInfo.visible"
      :id="licenseInfo.id"
      v-model:visible="licenseInfo.visible"
      @returnBack="getList"
    ></license-details>
    <product-details
      v-if="productInfo.visible"
      :id="productInfo.id"
      v-model:visible="productInfo.visible"
      @returnBack="getList"
    />
    <page-dialog
      v-if="dialogTipsInfo.visible"
      :title="dialogTipsInfo.title"
      v-model:visible="dialogTipsInfo.visible"
      :width="dialogTipsInfo.width"
      :bt-loading="dialogTipsInfo.btLoading"
      :bt-list="dialogTipsInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogTipsInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogTipsInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </page-dialog>
  </div>
</template>

<script setup name="productExpiryWarning">
import { productExpirePage, productExpireExcel } from '@/api/warning'
import { getDepotAll } from '@/api/core'
import { getCustomizeSetting } from '@/api/customize'
import LicenseDetails from './../../product/license/details'
import ProductDetails from './../../product/product/details'

const { proxy } = getCurrentInstance()
const route = useRoute()

const dialogTipsInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: true,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '打印',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})
const clearType = ref(true)
const slipSelectRequest = ref(true)
//颜色标识
const modelTipsInfo = ref({
  title: '效期状态',
  items: [
    {
      label: '近效期',
      style: { width: '10px', height: '10px', background: '#fceacb' },
    },
    {
      label: '过期',
      style: { width: '10px', height: '10px', background: '#f5e4e4' },
    },
  ],
})
const validityStatus = ref('')
const listTypeInfo = ref({
  typeList: [],
  depotList: [],
})
// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {
      depotAuth: {
        asTbName: 'core_depot',
        columnName: 'id',
        queryType: 'IN',
        queryCategory: 'AND',
        value: 'depotAuth',
      },
    },
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('productExpiryWarning:execl'),
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  fieldList: [],
})
const licenseInfo = ref({
  id: '',
  visible: false,
})
const productInfo = ref({
  id: '',
  visible: false,
})
const page = ref({
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})

watch(
  () => route.params,
  (val) => {
    const params = route.params
    const _paramsData = params.paramsData && JSON.parse(params.paramsData)
    if (
      params &&
      params.dataIndex !== undefined &&
      params.dataIndex !== null &&
      route.name === 'productExpiryWarning'
    ) {
      validityStatus.value = _paramsData.statusCode
      init()
    }
  },
  {
    immediate: true,
    deep: true,
  }
)

setData()
function setData() {
  if (!validityStatus.value) {
    init()
  }
}
function init() {
  let obj = {
    validityStatusValue: {
      label: '有效期状态',
      value: 'validityStatusValue',
      asValue: 'validityStatus',
      componentType: 'select-model',
      code: 'product_expire_warning_type',
      show: false,
      hidden: true,
      multiple: true,
    },
    price: {
      label: '单价',
      value: 'price',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('productExpiryWarning:price'),
      show: false,
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('productExpiryWarning:price'),
      show: false,
      hidden: true,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      componentType: 'price',
      type: 'column',
      value: 'defaultSupplierPrice',
      permission: !proxy.$permission('productExpiryWarning:price'),
      show: false,
      hidden: true,
    },
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
    licenseNumber: {
      label: '证号',
      value: 'licenseNumber',
      type: 'slot',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function clearSearch() {
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  filterInfo.value.query.queryMap.validityStatus.value = ''
  validityStatus.value = ''
  getList()
}
// 获取列表
function getList() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
//初始化仓库
function initDepotList() {
  const params = {
    depotAuth: 'id',
    useTypeId: '1',
    status: '1',
  }
  getDepotAll(params).then((resp) => {
    if (resp && resp.success) {
      listTypeInfo.value.depotList = resp.data.map((row) => {
        return {
          key: row.name,
          value: row.id,
        }
      })
    }
  })
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function showLicenseDetail(id) {
  licenseInfo.value.visible = true
  licenseInfo.value.id = id
}
function showProductDetail(id) {
  productInfo.value.visible = true
  productInfo.value.id = id
}
function tableRowClassName({ row = undefined }) {
  if (row.validityStatus === '近效期') {
    return 'nearly-row'
  } else if (row.validityStatus === '过期') {
    return 'expire-row'
  }
  return ''
}
async function getCustomize(_slipSelectRequest = true) {
  let _tableInfo = tableInfo.value
  let _filterInfo = filterInfo.value
  let param = {
    tableKey: 'productExpiryWaring-com.slip.spd.entity.vo.stock.DepotStockVO',
  }
  await getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        _filterInfo.searchList,
        data.detail
      )
      _filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ..._filterInfo.query.queryMap,
      }
      _filterInfo.searchList = searchInfo.searchList
      _filterInfo.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      if (validityStatus.value) {
        filterInfo.value.query.queryMap.validityStatus.value =
          validityStatus.value
      }
      slipSelectRequest.value = _slipSelectRequest
      _tableInfo.api = productExpirePage
      getList()
    }
  })
}
//导出
function exportExcelList() {
  let params = {
    ...filterInfo.value.query,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  productExpireExcel(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function exportExcel() {
  //导出
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'excel'
  dialogTipsInfo.value.msg = '请选择数据导出范围'
  dialogTipsInfo.value.btList[1].label = '导出'
  dialogTipsInfo.value.flag = true
}
function checkConfirm() {
  if (dialogTipsInfo.value.type === 'excel') {
    exportExcelList()
  } else if (dialogTipsInfo.value.type === 'print') {
    // printList();
  }
  dialogTipsInfo.value.visible = false
  dialogTipsInfo.value.msg = ''
}
function handleClose() {
  dialogTipsInfo.value.visible = false
}
//打印导出
function pageInfo(obj) {
  page.value = obj
}
</script>

<style scoped lang="scss">
:deep(.el-table) {
  .nearly-row {
    background: #fceacb;
  }
  .expire-row {
    background: #f5e4e4;
  }
}
</style>
